/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import javax.swing.*;

import blueprint4j.Constants;
import blueprint4j.utils.*;

import java.awt.event.*;
import javax.swing.event.*;

public abstract class DataPairDefault implements DataPair,FieldBinding,ActionListener,DocumentListener {
    
    private BindFieldInterface field = null;
    protected JComponent label=null;
    protected JComponent component=null;
    private Binder binder = null;
    
    private static String FONT_SIZE = Settings.getString("gui.font.size","normal");
    
    protected DataPairDefault(Binder binder,BindFieldInterface field){
        this.field = field;
        this.binder = binder;
    }
    
    public DataPairDefault(Binder binder,BindFieldInterface field,JComponent p_label,JComponent p_component){
        this(binder,field);
        setLabel(p_label);
        setComponent(p_component);
    }
    
    protected void setLabel(JComponent p_label){
        label=p_label;
        if (label==null){
            label=new JPanel();
        }
        if (label instanceof JLabel && !FONT_SIZE.endsWith("normal")) {
            if (FONT_SIZE.equals("small")) {
                label.setFont(new java.awt.Font(label.getFont().getFontName(),label.getFont().getStyle(),label.getFont().getSize()-2));
            }
            if (FONT_SIZE.equals("large")) {
                label.setFont(new java.awt.Font(label.getFont().getFontName(),label.getFont().getStyle(),label.getFont().getSize()+2));
            }
        }
    }
    
    protected void setComponent(JComponent p_component){
        component=p_component;
        if (component==null){
            component=new JPanel();
        }
        setVisible(field.getVisibleByDefault());
        addFieldRequireListener();
        determineFieldRequirement();
    }
    
    public JComponent getLabel(){
        return label;
    }
    
    public JComponent getComponent(){
        return component;
    }
    
    public void setVisible(boolean visible) {
        label.setVisible(visible);
        component.setVisible(visible);
    }
    
    public void insertUpdate(DocumentEvent event) {
        determineFieldRequirement();
    }
    
    protected Binder getBinder(){
    	return binder;
    }
    
    public void removeUpdate(DocumentEvent event) {
        determineFieldRequirement();
    }
    
    public void changedUpdate(DocumentEvent event) {
        determineFieldRequirement();
    }
    
    public void actionPerformed(ActionEvent event) {
        //System.out.println("Default Action Petrformed");
        determineFieldRequirement();
    }
    
    public void determineFieldRequirement() {
        try {
            for (int i=0;i < field.getFieldRequirement().size();i++) {
                binder.getVectorFieldBind(new VectorBindFieldInterface(new BindFieldInterface[]{field})).intoBindable();
                VectorFieldBinding binding = binder.getVectorFieldBind(field.getFieldRequirement().get(i).getVectorField());
                if (field.getFieldRequirement().get(i).determine(field)) {
                    binding.setVisible(field.getFieldRequirement().get(i).getVisible());
                } else {
                    binding.setVisible(!field.getFieldRequirement().get(i).getVisible());
                }
                // RESIZE THE PANE
                java.awt.Window parent = Gui.getParentWindow(component);
                if (parent != null) {
                    parent.pack();
                    parent.setLocationRelativeTo(parent.getParent());
                }
            }
        } catch (BindException be) {
            Log.debug.out(be);
        }
    }
    
    public void addFieldRequireListener() {
        if (field != null && field.getFieldRequirement().size() > 0) {
            if (component instanceof JTextField) {
                ((JTextField)component).getDocument().addDocumentListener(this);
            } else if (component instanceof JTextArea) {
                ((JTextArea)component).getDocument().addDocumentListener(this);
            } else if (component instanceof AbstractButton) {
                ((AbstractButton)component).addActionListener(this);
            } else if (component instanceof JComboBox) {
                ((JComboBox)component).addActionListener(this);
            }
        }
    }
    
}

